<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="执行时间" prop="queryTime">
        <el-date-picker
          v-model="dateRange"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"
        />
      </el-form-item>
      <el-form-item label="消耗时间" prop="elapsedTime">
        <el-input v-model="queryParams.elapsedTime" clearable placeholder="请输入最少消耗时间" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-has-permi="['system:sqlLog:remove']"
          type="danger"
          icon="Delete"
          plain
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-has-permi="['system:sqlLog:remove']"
          type="danger"
          icon="Delete"
          plain
          @click="handleClean"
        >清空</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-has-permi="['system:sqlLog:export']"
          type="warning"
          icon="Download"
          plain
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:show-search="showSearch" @query-table="getList" />
    </el-row>

    <el-table v-loading="loading" :data="sqlLogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="审计编号" align="center" prop="sqlId" />
      <el-table-column label="服务器 IP" align="center" prop="hostIp" />
      <el-table-column label="执行的 sql" align="center" prop="query" min-width="200" show-overflow-tooltip />
      <el-table-column label="参数" align="center" prop="queryParams" />
      <el-table-column label="执行时间" align="center" prop="queryTime" />
      <el-table-column label="消耗时间(毫秒)" align="center" prop="elapsedTime" />
      <el-table-column label="数据源" align="center" prop="dsName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            v-has-permi="['system:sqlLog:query']"
            type="primary"
            icon="View"
            link
            @click="handleView(scope.row)"
          >详细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      v-model:page="queryParams.pageNumber"
      v-model:limit="queryParams.pageSize"
      :total="total"
      @pagination="getList"
    />

    <!-- SQL审计详细对话框 -->
    <el-dialog v-model="open" title="SQL审计详细" width="500px" append-to-body>
      <el-form :model="form" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="服务器 IP：" prop="hostIp">{{ form.hostIp }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数据源：" prop="dsName">{{ form.dsName }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="执行的 sql：" prop="query">{{ form.query }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="参数：" prop="queryParams">{{ form.queryParams }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="执行时间：" prop="queryTime">{{ form.queryTime }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="消耗时间：" prop="elapsedTime">{{ form.elapsedTime }}毫秒</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="open = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script name="SqlLog" setup>
import { listSqlLog, delSqlLog, cleanSqlLog } from '@/api/system/sqlLog'

const { proxy } = getCurrentInstance()

const sqlLogList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const dateRange = ref([])

const data = reactive({
  form: {},
  queryParams: {
    pageNumber: 1,
    pageSize: 10,
    platform: undefined,
    module: undefined,
    url: undefined,
    user: undefined,
    userIp: undefined,
    hostIp: undefined,
    query: undefined,
    queryParams: undefined,
    queryTime: undefined,
    elapsedTime: undefined,
    dsName: undefined,
  }
})

const { queryParams, form } = toRefs(data)

/** 组装查询参数 */
function getQueryParams() {
  if (Array.isArray(dateRange.value)) {
    queryParams.value.startTime = dateRange.value[0]
    queryParams.value.endTime = dateRange.value[1]
  }
  return queryParams.value
}

/** 查询SQL审计列表 */
function getList() {
  loading.value = true
  listSqlLog(getQueryParams()).then(res => {
    sqlLogList.value = res.list
    total.value = Number(res.total)
    loading.value = false
  })
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNumber = 1
  getList()
}
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = []
  proxy.$resetForm('queryRef')
  handleQuery()
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.sqlId)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}
/** 详细按钮操作 */
function handleView(row) {
  open.value = true
  form.value = row
}
/** 删除按钮操作 */
function handleDelete(row) {
  const _sqlIds = row.sqlId || ids.value
  proxy.$modal.confirm('是否确认删除SQL审计编号为"' + _sqlIds + '"的数据项？').then(function() {
    loading.value = true
    return delSqlLog(_sqlIds)
  }).then(() => {
    loading.value = true
    getList()
    proxy.$modal.msgSuccess('删除成功')
  }).catch(() => {
  }).finally(() => {
    loading.value = false
  })
}
/** 清空按钮操作 */
function handleClean() {
  proxy.$modal.confirm('是否确认清空所有SQL审计数据项?').then(function() {
    return cleanSqlLog()
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess('清空成功')
  }).catch(() => {})
}
/** 导出按钮操作 */
function handleExport() {
  proxy.$download('system/sqlLog/export', {
    ...getQueryParams()
  }, `sqlLog_${new Date().getTime()}.xls`)
}

getList()
</script>
